<template>
  <div class='container'>
    <el-dialog  :title="logFormData.id ? '修改文章' : '新增文章'" :visible="showRoleDialog" @close="btnClose">
      <el-form label-width="80px" :model="logFormData"  :rules="rules"  ref="publicityForm">
        <el-form-item  label="文章标题"  prop="title">
          <el-input placeholder="请输入文章标题" v-model="logFormData.title"/>
        </el-form-item>
        <el-form-item  label="文章内容" style="margin-bottom:80px" prop="articleBody">
            <quill-editor
             style="height:200px;"
             v-model="logFormData.articleBody"
             @blur="changeQuillEditor"
             :options="editorOption"
             />
        </el-form-item>
        <el-form-item  label="视频地址" prop="videoURL">
          <el-input placeholder="请输入视频地址" v-model="logFormData.videoURL"/>
        </el-form-item>
      </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="end">
        <el-col :span="6">
          <el-button size="small" @click="btnClose">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import { add, update, detail } from '@/api/hmmm/articles'
export default {
  data () {
    return {
      logFormData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      editorOption: {
        placeholder: '请输入内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }

    }
  },
  props: {
    showRoleDialog: {
      required: true,
      type: Boolean
    }
  },
  components: {
    quillEditor
  },
  methods: {
    changeQuillEditor () {
      this.$refs.publicityForm.validateField('articleBody')
    },
    btnClose () {
      this.logFormData = {
        title: '',
        articleBody: '',
        videoURL: '',
        id: ''
      }
      this.$refs.publicityForm.resetFields()
      this.$emit('update:showRoleDialog', false)
    },
    async dingyiid (id) {
      const { data } = await detail(id)
      this.logFormData = data
    },
    async btnOk () {
      try {
        if (this.logFormData.id) {
          // 编辑
          await update(this.logFormData)
          this.$parent.createList()
        } else {
          // 添加
          await this.$refs.publicityForm.validate()
          const data = await add(this.logFormData)
          this.$parent.createList(data)
        }
        this.$message.success('操作成功')
        this.$emit('update:showRoleDialog', false)
      } catch (error) {
        this.$message.error('操作失败，请稍后再试!')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
